import sys
import os


sys.path.append(os.path.join(os.path.dirname(__file__), "/root/work/arcfox-crawler"))
import asyncio
from loguru import logger

from com.arcfox.school.spider.jzy.jzy_enrollment_plan_spider import EnrollmentPlanSpider
from com.arcfox.school.spider.jzy.jzy_yifen_yiduan_detail_spider import YifenYiduanDetailSpider
from com.arcfox.school.spider.jzy.jzy_yifen_yiduan_spider import YifenYiduanSpider
from com.arcfox.middleware import async_mysql_middleware as db
from com.arcfox.middleware.async_redis_middleware import open_redis
from com.arcfox.school.spider.jzy.jzy_major_score_spider import MajorScoreSpider
from com.arcfox.school.spider.jzy.jzy_school_detail_spider import SchoolDetailSpider
from com.arcfox.school.spider.jzy.jzy_school_list_spider import SchoolListSpider
from com.arcfox.school.spider.jzy.jzy_school_major_list_spider import SchoolMajorListSpider
from com.arcfox.util import date, constant
from com.arcfox.school.spider.jzy.jzy_province_score_spider import ProvinceScoreSpider
from com.arcfox.util.util import add_log


class SchoolDetailInfoService:
    def __init__(self):
        self.data_version = None
        self.school_list_spider = SchoolListSpider()
        self.school_detail_spider = SchoolDetailSpider()
        self.major_score_spider = MajorScoreSpider()
        self.school_major_list_spider = SchoolMajorListSpider()
        self.yifen_yiduan_list_spider = YifenYiduanSpider()
        self.yifen_yiduan_detail_list_spider = YifenYiduanDetailSpider()
        self.entroll_plan_list_spider = EnrollmentPlanSpider()
        self.province_university_score = ProvinceScoreSpider()
        add_log(self.__class__.__name__ + ".log")

    async def start(self):
        # self.data_version = await self.generate_data_version()
        logger.info("开始抓取学校列表数据...")
        await self.school_list_spider.start_crawl()
        # logger.info("学校列表数据抓取完成, 准备抓取学校详情数据")
        # await random_sleep(2)
        # await self.school_detail_spider.start_crawl()
        tasks = [
            # asyncio.create_task(self.major_score_spider.start_crawl()),
            # asyncio.create_task(self.yifen_yiduan_list_spider.start_crawl()),
            asyncio.create_task(self.entroll_plan_list_spider.start_crawl()),
            # asyncio.create_task(self.province_university_score.start_crawl()),

            # asyncio.create_task(self.school_major_list_spider.start_crawl()),
        ]
        await asyncio.gather(*tasks)
        # await self.yifen_yiduan_detail_list_spider.start_crawl()
        logger.info("数据抓取完成, 更新版本号")
        await self.update_data_version()

    @open_redis
    async def generate_data_version(self, client):
        data_version = date.format_date(date.now(), "%Y%m%d")
        await client.set(constant.CONFIG_KEY_DATA_VERSION, data_version)
        return date.format_date(date.now(), "%Y%m%d")

    async def update_data_version(self):
        mapping = db.MysqlMapping("tb_sys_config")
        await mapping.update({"config_value": self.data_version}, {"config_key": constant.CONFIG_KEY_DATA_VERSION})


if __name__ == "__main__":
    asyncio.run(SchoolDetailInfoService().start())
